package com.usian.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.behavior.dtos.FollowBehaviorDto;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.dtos.UserRelationDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserFan;
import com.usian.model.user.pojos.ApUserFollow;
import com.usian.user.feign.ApAuthorFeign;
import com.usian.user.mapper.ApUserFanMapper;
import com.usian.user.mapper.ApUserFollowMapper;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.service.ApUserRelationService;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserRelationServiceImpl implements ApUserRelationService {
    @Autowired
    private ApUserFanMapper apUserFanMapper;
    @Autowired
    private ApUserFollowMapper apUserFollowMapper;
    @Autowired
    private ApUserMapper apUserMapper;
    @Autowired
    private ApAuthorFeign apAuthorFeign;

    @Autowired
    private KafkaTemplate kafkaTemplate;

    @Override
    public ResponseResult follow(UserRelationDto dto) {
        //检验参数
        if (dto.getOperation()==null||dto.getOperation()<0||dto.getOperation()>1){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if(dto.getAuthorId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"作者信息不能为空");
        }
        //查询作者信息
        ApAuthor apAuthor = apAuthorFeign.selectById(dto.getAuthorId());
        //查询用户信息
        ApUser apUser = apUserMapper.selectById(apAuthor.getUserId());

        if (apAuthor!=null){
            if (apAuthor.getUserId()==null){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"关注人不存在");
            }else {
                //获取登录的用户id
                Integer userId = AppThreadLocalUtils.getUser().getId();
                if (userId!=null){
                    //0关注 1取消关注
                    if (dto.getOperation()==0){

                        if (apUser==null){
                            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"关注用户不存在");
                        }
                        //查询关注表是否存在
                        ApUserFollow apUserFollow = apUserFollowMapper.selectOne(Wrappers.<ApUserFollow>lambdaQuery().eq(ApUserFollow::getUserId, apUser.getId()).eq(ApUserFollow::getFollowId, apAuthor.getUserId()));
                        if (apUserFollow==null){
                            //查询粉丝是否存在
                            ApUserFan apUserFan = apUserFanMapper.selectOne(Wrappers.<ApUserFan>lambdaQuery().eq(ApUserFan::getUserId, apAuthor.getUserId()).eq(ApUserFan::getFansId, apUser.getId()));
                            if (apUserFan==null){
                                //保存粉丝表
                                apUserFan=new ApUserFan();
                                apUserFan.setUserId(apAuthor.getUserId());
                                apUserFan.setFansId(apUser.getId().longValue());
                                apUserFan.setFansName(apUser.getName());
                                apUserFan.setLevel((short)0);
                                apUserFan.setIsDisplay(true);
                                apUserFan.setIsShieldLetter(false);
                                apUserFan.setIsShieldComment(false);
                                apUserFan.setCreatedTime(new Date());
                                apUserFanMapper.insert(apUserFan);
                            }
                            //保存关注表
                            apUserFollow = new ApUserFollow();
                            apUserFollow.setUserId(apUser.getId());
                            apUserFollow.setFollowId(apAuthor.getUserId());
                            apUserFollow.setFollowName(apAuthor.getName());
                            apUserFollow.setCreatedTime(new Date());
                            apUserFollow.setIsNotice(true);
                            apUserFollow.setLevel((short)1);
                            apUserFollowMapper.insert(apUserFollow);
                            FollowBehaviorDto dto1 = new FollowBehaviorDto();
                            dto1.setFollowId(apAuthor.getUserId());
                            dto1.setArticleId(dto.getArticleId());
                            dto1.setUserId(apUser.getId());
                            //异步发送消息，保存关注行为
                            kafkaTemplate.send("kafka-behavior", JSON.toJSONString(dto));
                            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
                        }else {
                            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"已关注");
                        }
                    }else {
                        //取消关注
                        ApUserFollow apUserFollow = apUserFollowMapper.selectOne(Wrappers.<ApUserFollow>lambdaQuery().eq(ApUserFollow::getUserId, apUser.getId()).eq(ApUserFollow::getFollowId, apAuthor.getUserId()));
                        if (apUserFollow==null){
                            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"未关注");
                        }else {
                            ApUserFan apUserFan = apUserFanMapper.selectOne(Wrappers.<ApUserFan>lambdaQuery().eq(ApUserFan::getUserId, apAuthor.getUserId()).eq(ApUserFan::getFansId, apUser.getId()));
                            //删除用户粉丝信息
                            if (apUserFan!=null){
                                apUserFanMapper.delete(Wrappers.<ApUserFan>lambdaQuery().eq(ApUserFan::getUserId, apAuthor.getUserId()).eq(ApUserFan::getFansId, apUser.getId()));
                            }
                            apUserFollowMapper.delete(Wrappers.<ApUserFollow>lambdaQuery().eq(ApUserFollow::getUserId, apUser.getId()).eq(ApUserFollow::getFollowId, apAuthor.getUserId()));
                            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
                        }
                    }
                }
            }
        }
        return null;
    }
}
